package com.shopping;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        products.add(new Product("雪碧", "饮料", 3.0, 10));
        products.add(new Product("可乐", "饮料", 3.5, 15));
        products.add(new Product("果汁", "饮料", 4.0, 8));
        products.add(new Product("苹果", "水果", 2.0, 20));
        products.add(new Product("香蕉", "水果", 1.5, 12));
        ShoppingCart cart = new ShoppingCart();
        Scanner scanner = new Scanner(System.in);
        boolean shopping = true;
        while (shopping) {
            System.out.println("\n--- 购物车系统 ---");
            System.out.println("1. 查看商品列表");
            System.out.println("2. 添加商品到购物车");
            System.out.println("3. 查看购物车");
            System.out.println("4. 删除购物车中的商品");
            System.out.println("5. 结算");
            System.out.println("6. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.println("商品列表：");
                    for (Product product : products) {
                        System.out.println(product.getName() + " - " + product.getCategory() + " - 价格: " + product.getPrice() + " - 库存: " + product.getStock());
                    }
                    break;
                case 2:
                    System.out.print("输入要添加的商品名称：");
                    String productName = scanner.nextLine();
                    Product chosenProduct = null;
                    for (Product product : products) {
                        if (product.getName().equalsIgnoreCase(productName)) {
                            chosenProduct = product;
                            break;
                        }
                    }
                    if (chosenProduct != null) {
                        System.out.print("输入要添加的数量：");
                        int quantity = scanner.nextInt();
                        cart.addProduct(chosenProduct, quantity);
                    } else {
                        System.out.println("商品未找到！");
                    }
                    break;
                case 3:
                    cart.viewCart();
                    break;
                case 4:
                    System.out.print("输入要删除的商品名称：");
                    productName = scanner.nextLine();
                    chosenProduct = null;
                    for (Product product : products) {
                        if (product.getName().equalsIgnoreCase(productName)) {
                            chosenProduct = product;
                            break;
                        }
                    }
                    if (chosenProduct != null) {
                        cart.removeProduct(chosenProduct);
                    } else {
                        System.out.println("商品未找到！");
                    }
                    break;
                case 5:
                    double total = cart.checkout();
                    System.out.println("结算总额：" + total);
                    break;
                case 6:
                    shopping = false;
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
        scanner.close();
    }
}